import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import VueSetupExtend from "vite-plugin-vue-setup-extend";
// import { nodePolyfills } from "vite-plugin-node-polyfills";
import { resolve } from "path";

// import dynamicImport from "vite-plugin-dynamic-import";
import { viteCommonjs } from "@originjs/vite-plugin-commonjs";

const pathResolve = dir => {
	return resolve(__dirname, ".", dir);
};

const alias = {
	"@": pathResolve("src")
};

// https://vite.dev/config/
// export default defineConfig({
//   plugins: [vue()],
// })

export default defineConfig(({ command, mode }) => {
	// 获取.env文件里定义的环境变量
	const env = loadEnv(mode, process.cwd());
	//console.log(env);   //变量在命令行里打印出来
	//.env文件中的环境变量必须以VITE_为前缀，否则无法引用成功
	const { VITE_BASE } = env;

	console.log("VITE_BASE:", VITE_BASE);

	return {
		base: VITE_BASE,
		assetsInclude: ["**/*.wasm"],
		plugins: [vue(), VueSetupExtend(), viteCommonjs()],
		optimizeDeps: {
			exclude: [
				"@cornerstonejs/dicom-image-loader",
				"@cornerstonejs/polymorphic-segmentation"
			],
			include: ["dicom-parser"]
		},
		worker: {
			format: "es",
			rollupOptions: {
				external: ["@icr/polyseg-wasm"]
			}
		},
		build: {
			outDir: "webdicom",
			emptyOutDir: true
		},
		resolve: {
			alias,
			extensions: [".vue", ".js"]
		},
		server: {
			host: "0.0.0.0",
			port: 4000
		}
	};
});

// export default defineConfig({
// 	assetsInclude: ["**/*.wasm"],
// 	plugins: [vue(), VueSetupExtend(), viteCommonjs()],
// 	optimizeDeps: {
// 		exclude: [
// 			"@cornerstonejs/dicom-image-loader",
// 			"@cornerstonejs/polymorphic-segmentation"
// 		],
// 		include: ["dicom-parser"]
// 	},
// 	worker: {
// 		format: "es",
// 		rollupOptions: {
// 			external: ["@icr/polyseg-wasm"]
// 		}
// 	},
// 	build: {
// 		outDir: "webdicom",
// 		emptyOutDir: true
// 	},
// 	resolve: {
// 		alias: {
// 			"@": "/src"
// 		},
// 		extensions: [".vue", ".js"]
// 	},
// 	server: {
// 		host: "0.0.0.0",
// 		port: 4000
// 	}
// });
